
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "dna_json.h"


#define tc_log(_fmt_, ...)          printf("[TC][%s:%d][%s] "_fmt_"\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__)
#define tc_log_err(_fmt_, ...)      printf("[TC][ERR][%s:%d][%s] "_fmt_"\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__)


static void test1(void)
{
	dna_json_object_t jobj;
	char buf[256];

    dna_json_str_init(&jobj, buf, sizeof(buf));
	dna_json_start_object(&jobj);
	{
		dna_json_push_object(&jobj, "privdata");
		dna_json_set_val_str(&jobj, "mtag", "xx");
		dna_json_close_object(&jobj);

		dna_json_push_object(&jobj, "notify");
		dna_json_set_val_str(&jobj, "devType", "xx");
		dna_json_close_object(&jobj);

		dna_json_set_val_str(&jobj, "devinfo", "xx");
	}
	dna_json_close_object(&jobj);

	int jsize = dna_json_str_len(&jobj);

	tc_log("final json len %d: %.*s", jsize, jsize, buf);
}

static void test2(void)
{
	// const char input[] = "{\"pwr.0\":0,\"name\":\"jolina\",\"brightness.1\":60}";
	// char snapshot[128] = "{\"pwr.0\":1,\"pwr.1\":1,\"brightness.0\":16,\"brightness.1\":90,\"name\":\"ted\"}";

	const char input[] = "{\"pwr1\":0,\"pwr2\":1,\"pwr3\":0}";
	char snapshot[128] = "{\"version\":54016,\"pwr1\":1,\"pwr2\":0,\"pwr3\":1}";
	
	int newsnapsize = snapshot_update(snapshot, strlen(snapshot), sizeof(snapshot), input);
	tc_log("new snapshot %d:%.*s", newsnapsize, newsnapsize, snapshot);
}

static void test3(void)
{
	const char input[] = "{\"pwr1\":1,\"pwr2\":1,\"pwr3\":1,\"plugmode\":0,\"pwr\":0}";
	dna_json_object_t jobj;
	int val;
	
	dna_json_object_init(&jobj, input);
	dna_json_get_val_int(&jobj, "pwr", &val);

	tc_log("get pwr %d", val);
}


static int bl2_keyvalue_upload_check(char * packet, int packet_len, int free_size)
{
    char temp[33] = {0};
    int left = 0;
    int mask = 0, append_len = 0;

#define NEED_APPEND_PID (1<<0)
#define NEED_APPEND_DID (1<<1)

    /* First raw packet must is JSON */
    if(packet[0] != '{' || packet[packet_len - 1] != '}')
        return packet_len;

    /* Try to append '\0' */
    if(free_size <= 1)
        return packet_len;

    packet[packet_len] = '\0';

    /* Try to append PID */
    if(!strstr(packet, "pid"))
    {
        mask |= NEED_APPEND_PID;
        /* ,"pid":" + str + " */
        append_len += sizeof("\"pid\":\"") + 33;
    }

    /* Try to append DID */
    if(!strstr(packet, "did"))
    {
        mask |= NEED_APPEND_DID;
        /* ,"did":" + str + " */
        append_len += sizeof("\"did\":\"") + 33;
    }

    /* Check free size whether enough */
    if(free_size < append_len)
        return packet_len;

    tc_log("packet_len %d mask %x", packet_len, mask);

    if (mask != 0)
        left = 1;

    append_len = 0;

    if(mask & NEED_APPEND_PID)
    {
        append_len += sprintf(packet + packet_len - left, ",\"pid\":\"%s\"", "00000000000000000000000068750000");
        // tc_log("append_len %d packet %s", append_len, packet);
    }

    if(mask & NEED_APPEND_DID)
    {
        append_len += sprintf(packet + packet_len - left + append_len, ",\"did\":\"00000000000000000000%s\"", "c8f742fe5daa");
    }

    append_len += sprintf(packet + packet_len - left + append_len, "}");

    return packet_len - left + append_len;
}

static void test4(void)
{
	// char t_str[] = "{\"pwr1\":1,\"pwr2\":1,\"pwr3\":1,\"plugmode\":0,\"hb_timeout\":3600,\"did\":\"00000000000000000000c8f742fe5d85\",\"offline\":0}";
	char t_str[] = "{\"pwr1\":1,\"pwr2\":1,\"pwr3\":1,\"plugmode\":0,\"hb_timeout\":3600,\"offline\":0}";
	char buff[1024];

	strcpy(buff, t_str);
	tc_log("buff before %s", buff);
	int ret = bl2_keyvalue_upload_check(buff, strlen(buff), sizeof(buff) - strlen(buff));
	tc_log("ret %d buff after %s", ret, buff);
}

int main(int argc, char const *argv[])
{
	tc_log("********************");
	tc_log("new start @ %s %s", __DATE__, __TIME__);
	tc_log("********************");

	test4();	

	tc_log("exit");
	return 0;
}

